﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LoU.Dungeons;
using System.Globalization;

namespace Lou.Dungeons.WebUI
{
    public partial class _Default : System.Web.UI.Page
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            if (!Page.IsPostBack)
            {
                ddlUnitType1.DataSource = Enum.GetValues(typeof(LoU.Dungeons.UnitType));
                ddlDungeonType.DataSource = Enum.GetValues(typeof(DungeonType));
                UpdateDdls(LoU.Dungeons.UnitType.Berserker);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        int ratioMax;
        public void btnCalculate_Click(object sender, EventArgs e)
        {
            LoU.Dungeons.UnitType u1 = (LoU.Dungeons.UnitType)Enum.Parse(typeof(LoU.Dungeons.UnitType), ddlUnitType1.SelectedValue);
            LoU.Dungeons.UnitType u2 = (LoU.Dungeons.UnitType)Enum.Parse(typeof(LoU.Dungeons.UnitType), ddlUnitType2.SelectedValue);
            DungeonType dungeon = (DungeonType)Enum.Parse(typeof(DungeonType), ddlDungeonType.SelectedValue);
            
            IEnumerable<RatioGain> res =
                Compute.GainPerTS(
                    u1, int.Parse(tbResearch1.Text),
                    u2, int.Parse(tbResearch2.Text, CultureInfo.InvariantCulture),
                    double.Parse(tbRoundTripTime.Text, CultureInfo.InvariantCulture), dungeon, 0.60,
                    cbForceBuild.Checked ? 
                        Data.CastleBuilds.FirstOrDefault(b => b.ToString().Equals(ddlBuild.SelectedValue, StringComparison.InvariantCultureIgnoreCase))
                        : null);
            ratioMax = res.Where(a => a.TotalWithoutGoldPerHour == res.Max(b => b.TotalWithoutGoldPerHour)).First().Ratio;
            gvResults.DataSource = res.Where(r => new double[] { 0, ratioMax, 100, 50, 25, 75, 33, 66 }.Contains(r.Ratio));

            //foreach (DataGridViewRow row in dgv1.Rows)
            //{
            //    if ((row.DataBoundItem as RatioGain).Ratio == ratioMax)
            //    {
            //        foreach (DataGridViewCell cell in row.Cells)
            //        {
            //            cell.Style.Font = new Font(cell.InheritedStyle.Font, FontStyle.Bold);
            //        }
            //    }
            //}
        }

        public void ddlUnitType1_SelectedIndexChanged(object sender, EventArgs e)
        {
            UpdateDdls((LoU.Dungeons.UnitType)Enum.Parse(typeof(LoU.Dungeons.UnitType), ddlUnitType1.SelectedValue));
        }

        private void UpdateDdls(LoU.Dungeons.UnitType unit)
        {
            ddlBuild.DataSource = Data.CastleBuilds.Where(b => b.AvailableUnits.Contains(unit)).ToList();
            ddlUnitType2.DataSource = Data.CastleBuilds.Where(b => b.AvailableUnits.Contains(unit)).SelectMany(b => b.AvailableUnits).Distinct().ToList();
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            ddlDungeonType.DataBind();
            ddlUnitType1.DataBind();
            ddlUnitType2.DataBind();
            ddlBuild.DataBind();
            gvResults.DataBind();

            if (!Page.IsPostBack)
            {
                ddlDungeonType.SelectedIndex = 0;
                ddlUnitType1.SelectedIndex = 0;
                ddlUnitType2.SelectedIndex = 2;
                tbResearch1.Text = "0";
                tbResearch2.Text = "0";
                tbRoundTripTime.Text = "4";
            }
        }

        public void gvResults_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            switch (e.Row.RowType)
            {
                case DataControlRowType.Header:
                    e.Row.Cells[0].Text = "% " + ddlUnitType1.SelectedValue;
                    break;
                case DataControlRowType.DataRow:
                    if ((e.Row.DataItem as RatioGain).Ratio == ratioMax)
                    {
                        e.Row.CssClass = "MaxRatio";
                    }
                    break;
            }
        }
    }
}
